﻿using System.Text.RegularExpressions;

namespace SPFluid.Core.Validation.Extensions
{
    public static class StringValidatorExtensions
    {
        public static IGuard<string> IsNotEmpty(this IGuard<string> guard)
        {
            if (guard.Argument.Value == string.Empty)
            {
                guard.ExceptionHandler.ThrowArgumentException("String is empty");
            }

            return guard;
        }

        public static IGuard<string> IsNotNullOrEmpty(this IGuard<string> guard)
        {
            if (string.IsNullOrEmpty(guard.Argument.Value))
            {
                guard.ExceptionHandler.ThrowArgumentException("String is null or empty");
            }

            return guard;
        }

        public static IGuard<string> StartsWith(this IGuard<string> guard, string value)
        {
            if (!guard.Argument.Value.StartsWith(value))
            {
                guard.ExceptionHandler.ThrowArgumentException(string.Format("String must start with '{0}'", value));
            }

            return guard;
        }

        public static IGuard<string> EndsWith(this IGuard<string> guard, string value)
        {
            if (!guard.Argument.Value.EndsWith(value))
            {
                guard.ExceptionHandler.ThrowArgumentException(string.Format("String must end with '{0}'", value));
            }

            return guard;
        }

        public static IGuard<string> Length(this IGuard<string> guard, int length)
        {
            if (guard.Argument.Value.Length != length)
            {
                guard.ExceptionHandler.ThrowArgumentException("String have wrong length");
            }

            return guard;
        }

        public static IGuard<string> Contains(this IGuard<string> guard, string value)
        {
            if (!guard.Argument.Value.Contains(value))
            {
                guard.ExceptionHandler.ThrowArgumentException(string.Format("String must contain '{0}'", value));
            }

            return guard;
        }

        public static IGuard<string> IsMatch(this IGuard<string> guard, string pattern)
        {
            if (!new Regex(pattern).IsMatch(guard.Argument.Value))
            {
                guard.ExceptionHandler.ThrowArgumentException(string.Format("String must match '{0}'", pattern));
            }

            return guard;
        }
    }
}